package com.baidu.controller;

import com.baidu.bean.User;
import com.baidu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Mawq
 * @Date 2021/9/2 15:06
 * @Version 1.0
 */
@RestController
@RequestMapping(value = "/api")
public class UserRestController {

    @Autowired
    private IUserService userService;

    @RequestMapping(value = "/user", method = RequestMethod.POST)
    public boolean addUser(User user) {
        System.out.println("开始新增...");
        return userService.addUser(user);
    }

    @PutMapping("/user")
    public boolean updateUser(User user) {
        System.out.println("开始更新...");
        return userService.updateUser(user);
    }

    @DeleteMapping("/user/{id}")
    public boolean delete(@PathVariable Integer id) {
        System.out.println("开始删除...");
        return userService.deleteUser(id);
    }

    @PostMapping("/all")
    public Boolean delectUserByIds(@RequestParam("id") String ids) {
        System.out.println("开始批量删除...");

        List<String> dList = new ArrayList<String>();
        String[] strs = ids.split(",");
        for (String str : strs) {
            dList.add(str);
        }
        return null;

    }


    @GetMapping("/user")
    public User findByUserName(@RequestParam(value = "userName", required = true) String userName) {
        System.out.println("开始查询...");
        return userService.findUserByName(userName);
    }

    @GetMapping("/userAll")
    public List<User> findByUserAge() {
        System.out.println("开始查询所有数据...");
        return userService.findAll();
    }
}
